from pyleo.model.Resource import resource
from pylons import g
import os
def lfs_aggregate(lfs,path,limit=None):
    ''' Take the lfs files at path, and aggregate their wiki content into one or more lists of 
    dictionaries the required content type '''
    if limit==0: return [],0
    dirs,files=g.lfs.get_children(path.strip('/'))
    aggregation=[]
    for f in files:
        fullPath=os.path.join(path,f)
        r=resource(fullPath)
        if r.content_type.startswith('wiki') or r.content_type=='xhtml':
            rr=r.get()
            rr['nComments']=len(r.get_annotations('comment'))
            rr['nTrackbacks']=len(r.get_annotations('trackback'))
            rr['url']=fullPath
            aggregation.append(rr)
            if limit!= None:limit-=1
            if limit==0: return aggregation,limit
    for d in dirs:
        fullPath=os.path.join(path,d)
        a,limit=lfs_aggregate(lfs,fullPath,limit)
        aggregation+=a
    return aggregation,limit
    